package com.windea.study.kotlin.game.tankwar1.core

import javafx.scene.canvas.*
import javafx.scene.image.*
import javafx.scene.paint.*
import javafx.scene.text.*

object Painter {
	private lateinit var gc: GraphicsContext
	//图片缓存
	private val imageCache = mutableMapOf<String, Image>()
	
	internal fun set(graphicsContext: GraphicsContext) {
		gc = graphicsContext
	}
	
	/**
	 * 获取图片大小
	 */
	fun size(imagePath: String): Array<Int> {
		val cache = imageCache[imagePath]
		if(cache != null) arrayOf(cache.width.toInt(), cache.height.toInt())
		
		val image = Image(imagePath)
		imageCache.put(imagePath, image)
		return arrayOf(image.width.toInt(), image.height.toInt())
	}
	
	/**
	 * 绘制图片
	 */
	fun drawImage(imagePath: String, x: Int, y: Int) {
		val cache = imageCache[imagePath]
		if(cache != null) gc.drawImage(cache, x.toDouble(), y.toDouble())
		
		val image = Image(imagePath)
		imageCache.put(imagePath, image)
		gc.drawImage(image, x.toDouble(), y.toDouble())
	}
	
	/**
	 * 绘制颜色
	 */
	fun drawColor(color: Color, x: Int, y: Int, width: Int, height: Int) {
		gc.fill = color
		gc.fillRect(x.toDouble(), y.toDouble(), width.toDouble(), height.toDouble())
		gc.fill = Color.BLACK
	}
	
	/**
	 * 绘制文本
	 */
	fun drawText(text: String, x: Int, y: Int, color: Color = Color.BLACK, font: Font = Font.getDefault()) {
		gc.fill = color
		gc.font = font
		gc.fillText(text, x.toDouble(), y.toDouble())
		gc.font = Font.getDefault()
		gc.fill = Color.BLACK
	}
	
}
